package com.ibm.ops;

import java.io.File;
import java.util.Date;

import org.apache.log4j.Logger;

import com.ibm.ops.conf.ProjectConfiguration;
import com.ibm.ops.conf.ProjectConfiguration.ProjectConfigurationDomain;
import com.ibm.ops.domain.OpsAuditLogMetaData;
import com.ibm.ops.exception.CreateReportException;
import com.ibm.ops.exception.InvalidPropertiesException;
import com.ibm.ops.exception.NoPropConfigurationException;
import com.ibm.ops.exception.NoPropLogParserException;
import com.ibm.ops.exception.PersistentIOException;
import com.ibm.ops.exception.ReadAuditLogIOException;
import com.ibm.ops.exception.RetrieveReportDataException;
import com.ibm.ops.parser.Parser;
import com.ibm.ops.parser.ParserFactory;
import com.ibm.ops.persistent.XLSV03PersistentHandler;

public class Main {
	
	private static Logger logger = Logger.getLogger(Main.class);

	public static void main(String[] args) {	
		
		System.out.println("[" + new Date() + "] "+ " Start ops audit log tool. Version[" + OpstoolConstant.TOOL_VERSION + "].");
		logger.info("Start ops audit log tool. Version[" + OpstoolConstant.TOOL_VERSION + "]." + new Date());
			
		if(args.length < 1){
			System.out.println("please config the properites file path. System exit.");
			System.exit(-1);
		}
		
		try {	
			//load configuration here first.
			ProjectConfiguration.getInstance(args[0]);
						
			OpsAuditLog log = new OpsAuditLog(ProjectConfiguration.getInstance().getProjectConfiguration().getRaw_log_path());
			
			OpsAuditLogStore store = new OpsAuditLogStore();
			store.registerOpsLogStoreListener(new OpsAuditLogStoreListener(){
//				@Override
				public void onAddAuditLogMeta(OpsAuditLogMetaData logMeta) {
					try {
						logger.info("start to parse log file. type : " + logMeta.getType() + ",processing..");
						System.out.println("Parsing log file...please wait.[" + logMeta.getType() + "].");
						Parser parser = ParserFactory.createParserInstance(logMeta);						
						if(parser != null){
//							parser.setPersistentHanlder(new CSVPersistentHandler(Main.getPersistentPath(logMeta)));
							parser.setPersistentHanlder(new XLSV03PersistentHandler());
							parser.parse(logMeta.getContent());
						}
						logger.info("end to parse log file. type : " + logMeta.getType() + ".");
						System.out.println("End parse log file.[" + logMeta.getType() + "].");
					} catch (NoPropLogParserException e) {
						System.out.println(e.getMessage());
					} catch (CreateReportException e) {
						System.out.println(e.getMessage());
					} catch (RetrieveReportDataException e) {
						System.out.println(e.getMessage());
					} catch (PersistentIOException e) {
						System.out.println(e.getMessage());
					}					
				}				
			});				
			log.setStore(store);
			log.parseLog();
			
			
		} catch (NoPropConfigurationException e1) {
			logger.error("Can not find the configuration properties file. The default path is cfg/conf.properties.\nSystem exit.");
			System.out.println("Can not find the configuration properties file. The default path is cfg/conf.properties.\nSystem exit.");
			System.exit(-1);
		} catch (InvalidPropertiesException e) {
			logger.error("Invalid properites files." + e.getMessage() + ". Please check the configuration file.\nSystem exit.");
			System.out.println("Invalid properites files." + e.getMessage() + ". Please check the configuration file.\nSystem exit.");
			System.exit(-1);
		} catch (ReadAuditLogIOException e) {
			logger.error("Error happened when loading the audit log. please contact the tool owner.\n System exit.");
			System.exit(-1);
		}		
	}

	private static String getPersistentPath(OpsAuditLogMetaData logMeta) {
		String path = "";
		ProjectConfigurationDomain domain;
		try {
			domain = ProjectConfiguration.getInstance().getProjectConfiguration();
			switch(logMeta.getType()){
			case HUB:
				path = domain.getDetination_path() + File.separator + domain.getHub_report_name();
				break;
			case INFOSOURCE:
				path = domain.getDetination_path() + File.separator + domain.getInfosource_report_name();
				break;
			case CIR:
				path = domain.getDetination_path() + File.separator + domain.getCir_report_name();
				break;
			case FODS:
				path = domain.getDetination_path() + File.separator + domain.getFods_report_name();
				break;
			case FODS_REJECT:
				path = domain.getDetination_path() + File.separator + domain.getFods_reject_report_name();
				break;
			case FODS_OUTOF_SEQUENCE:
				path = domain.getDetination_path() + File.separator + domain.getFods_outof_seq_report_name();
				break;
			case FODS_NO_ORDER:
				path = domain.getDetination_path() + File.separator + domain.getFods_noorder_report_name();
				break;
			case FODS_EOD:
				path = domain.getDetination_path() + File.separator + domain.getFods_eod_report_name();
				break;
			case FOOT:
				path = domain.getDetination_path() + File.separator + domain.getFoot_report_name();
				break;
			default:
				break;
			}
		} catch (NoPropConfigurationException e) {
			//ignore this
		} catch (InvalidPropertiesException e) {
			
		}
		
		return path;
	}
	
}
